package Classic150;

public class 买卖股票的最佳时机4 {
    /*
        3的进阶版，拓展状态即可
     */
    public int maxProfit(int k, int[] prices) {
        int[] dp = new int[2 * k + 1];
        for (int i = 1; i <= k; i++) {
            dp[2 * i - 1] = -prices[0];
        }
        for (int i = 1; i < prices.length; i++) {
            for (int j = 1; j <= k; j++) {
                dp[2 * j - 1] = Math.max(dp[2 * (j - 1)] - prices[i], dp[2 * j - 1]);
                dp[2 * j] = Math.max(dp[2 * j - 1] + prices[i], dp[2 * j]);
            }
        }
        return dp[2 * k];
    }
}
